import { getBankDetail, getBranchBankDetail } from '@/api/bank'
import { getAreatext } from '@/api/area'

const state = () => {
  return {
    openPermissionImg: '', //开户许可证
    settleFontImg: '', //
    settleBackImg: '', //
    settleHandFontImg:'', //加结算人手持身份证照
    bankCardImg: '', //银行卡正面
    bankCard2Img: '', //银行卡反面

    accountCardImg:'',

    accountName1: '',
    checkApproveNo:'',  //核准号
    accountNum1: '',
    accountMobile1: '',
    bankId1: '',
    bankId1Text: '',
    acconutArea1: [],
    acconutArea1Text: '',
    branchBankId1: '',
    branchBankId1text: '',

    acconutType: '', //账户类型
    accountTypeText: '',
    settlementName: '',
    settlementNum: '',
    idCardStartTime: '',
    idCardEndTime: '',

    accountName2: '',
    accountNum2: '',
    bankCardNo:'',
    accountMobile2: '',
    bankAccountMobile:'',
    bankId2: '',
    bankId2Text: '',
    bankIdHo:'',
    bankIdHoText:'',
    acconutArea2: [],
    acconutArea2Text: '',
    acconutArea3:[],
    acconutArea3Text:'',
    branchBankId2: '',
    branchBankId2Text: '',
    txType:'',

    is_step2: true,
    no_step2: false,
  }
}

const mutations = {
  PSetOpenPermissionImg(state, url) {
    state.openPermissionImg = url
  },
  PSetSettleFontImg(state, url) {
    state.settleFontImg = url
  },
  PSetSettleHandFontImg(state, url) {
    state.settleHandFontImg = url
  },
  PSetSettleBackImg(state, url) {
    state.settleBackImg = url
  },
  PSetBankCardImg(state, url) {
    state.bankCardImg = url
  },
  PSetBankCard2Img(state, url) {
    state.bankCard2Img = url
  },
  PSetAccountCardImg(state, url) {
    state.accountCardImg = url
  },
  PSetAccountCard2Img(state, url) {
    state.accountCard2Img = url
  },
  ISetAccountName1(state, data) {
    state.accountName1 = data
  },
  ISetCheckApproveNo(state, data) {
    state.checkApproveNo = data
  },

  ISetAccountNum1(state, data) {
    state.accountNum1 = data
  },
  ISetAccountMobile1(state, data) {
    if (!/^0?(13|14|15|16|17|18|19)[0-9]{9}$/.test(data)) {
      state.accountMobile1 = ''
    } else {
      state.accountMobile1 = data
    }
  },
  ISetSettlementName(state, data) {
    if (/^[\u4E00-\u9fa5a-zA-Z\uf900-\ufa2d·s]{1,225}$/.test(data)) {
      state.settlementName = data
    } else {
      state.settlementName = ''
    }
  },
  ISetSettlementNum(state, data) {
    state.settlementNum = data
  },
  ISetAccountName2(state, data) {
    state.accountName2 = data
  },
  ISetAccountNum2(state, data) {
    state.accountNum2 = data
  },
  ISetBankCardNo(state, data) {
    state.bankCardNo = data
  },
  ISetAccountMobile2(state, data) {
    if (!/^0?(13|14|15|16|17|18|19)[0-9]{9}$/.test(data)) {
      state.accountMobile2 = ''
    } else {
      state.accountMobile2 = data
    }
  },
  ISetBankAccountMobile(state, data) {
    if (!/^0?(13|14|15|16|17|18|19)[0-9]{9}$/.test(data)) {
      state.bankAccountMobile = ''
    } else {
      state.bankAccountMobile = data
    }
  },
  setAccountType(state, data) {
    state.accountTypeText = data.text
    state.acconutType = data.value
  },
  setBankId1(state, data) {
    state.bankId1 = data.value
    state.bankId1Text = data.text
  },
  setAccountArea1(state, data) {
    state.acconutArea1 = data.acconutArea1
    state.acconutArea1Text = data.acconutArea1Text
  },
  setBranchBankId1(state, data) {
    state.branchBankId1text = data.bankName
    state.branchBankId1 = data.id
  },
  setIdCardStartTime(state, data) {
    state.idCardStartTime = data
  },
  setIdCardEndTime(state, data) {
    state.idCardEndTime = data
  },
  setBankId2(state, data) {
    state.bankId2 = data.value
    state.bankId2Text = data.text
  },
  setBankIdHo(state, data) {
    console.log('datassss',data)
    state.bankIdHo = data.value
    state.bankIdHoText = data.text
  },
  setAccountArea2(state, data) {
    state.acconutArea2 = data.acconutArea2
    state.acconutArea2Text = data.acconutArea2Text
  },
  setAccountArea3(state, data) {
    state.acconutArea3 = data.acconutArea3
    state.acconutArea3Text = data.acconutArea3Text
  },
  setBranchBankId2(state, data) {
    state.branchBankId2Text = data.bankName
    state.branchBankId2 = data.id
  },
  setBankId(state, data) {
    state.bankIdText = data.bankName
    state.bankId = data.id
  },
  setStep2(state, data) {
    let accountTypeObj = {
      '1': '法人对公',
      '2': '法人对私',
      '3': '非法人对私',
    }
    if (data.storeAccountType && data.storeAccountType != '0') {
      state.acconutType = data.storeAccountType
      state.accountTypeText = accountTypeObj[String(data.storeAccountType)]
    }
    if (data.storeAccount.accountIdcardExp) {
      state.idCardStartTime = data.storeAccount.accountIdcardExp.split(',')[0]
      state.idCardEndTime = data.storeAccount.accountIdcardExp.split(',')[1]
    }
    state.settlementName = data.storeAccount.accountName
    state.settlementNum = data.storeAccount.accountIdcard
    state.bankCardImg = data.storeAccount.cardImg
    state.bankCard2Img=data.storeAccount.card2Img
    state.checkApproveNo=data.storeAccount.checkApproveNo
    // state.idcard1Img
    //  state.idcard2Img
    data.storeAccountAttach.forEach((i) => {
      switch (i.fileType) {
        case '13':
          state.openPermissionImg = i.filePath
          break
        case '5':
          state.bankCardImg = i.filePath
          break
        case '27':
          state.bankCard2Img = i.filePath
          break
        case '6':
          state.settleFontImg = i.filePath
          break
        case '7':
          state.settleBackImg = i.filePath
          break
        case '28':
          state.settleHandFontImg = i.filePath
        break

      }
    })

    if (
      (data.storeType == 1 && data.storeAccountType == 2) ||
      (data.storeType == 1 && data.storeAccountType == 3)
    ) {
      state.accountName2 = data.storeAccount.accountName
      state.accountNum2 = data.storeAccount.accountNo
      state.bankId2 = data.storeAccount.bankIdHo
      state.branchBankId2 = data.storeAccount.bankId
      state.accountMobile2 = data.storeAccount.bankAccountMobile

      state.accountName1 = data.storeAccount.companyAccountName
      state.accountNum1 = data.storeAccount.companyAccountNo
      state.bankId1 = data.storeAccount.companyBankIdHo
      state.branchBankId1 = data.storeAccount.companyBankId
      state.accountMobile1 = data.storeAccount.companyAccountMobile
      // 公户总行
      if (
        data.storeAccount.companyBankIdHo &&
        data.storeAccount.companyBankIdHo != '0'
      ) {
        getBankDetail(data.storeAccount.companyBankIdHo).then((res2) => {
          if (res2.code == 200) {
            state.bankId1Text = res2.data.bankName
          }
        })
      }
      // 公户支行
      if (
        data.storeAccount.companyBankId &&
        data.storeAccount.companyBankId != '0'
      ) {
        getBranchBankDetail(data.storeAccount.companyBankId).then((res3) => {
          if (res3.code == 200) {
            state.branchBankId1text = res3.data.bankName
          }
        })
      }
      // 私户总行
      if (data.storeAccount.bankIdHo && data.storeAccount.bankIdHo != '0') {
        getBankDetail(data.storeAccount.bankIdHo).then((res4) => {
          if (res4.code == 200) {
            state.bankId2Text = res4.data.bankName
          }
        })
      }
      // 私户支行
      if (data.storeAccount.bankId && data.storeAccount.bankId != '0') {
        getBranchBankDetail(data.storeAccount.bankId).then((res5) => {
          if (res5.code == 200) {
            state.branchBankId2Text = res5.data.bankName
          }
        })
      }
      // 公户省市
      if (data.storeAccount.companyAccountProvince) {
        state.acconutArea1 = [
          data.storeAccount.companyAccountProvince,
          data.storeAccount.companyAccountCity,
          data.storeAccount.companyAccountDistrict,
        ]
        getAreatext({
          provinceCode: data.storeAccount.companyAccountProvince,
          cityCode: data.storeAccount.companyAccountCity,
          districtCode: data.storeAccount.companyAccountDistrict,
        }).then((res6) => {
          if (res6.code == 200) {
            const { province } = res6.data
            const { city } = res6.data
            const { district } = res6.data
            state.acconutArea1Text = province + city + district
          }
        })
      }
      // 私户省市
      if (data.storeAccount.accountProvince) {
        state.acconutArea2 = [
          data.storeAccount.accountProvince,
          data.storeAccount.accountCity,
          data.storeAccount.accountDistrict,
        ]
        getAreatext({
          provinceCode: data.storeAccount.accountProvince,
          cityCode: data.storeAccount.accountCity,
          districtCode: data.storeAccount.accountDistrict,
        }).then((res6) => {
          if (res6.code == 200) {
            const { province } = res6.data
            const { city } = res6.data
            const { district } = res6.data
            state.acconutArea2Text = province + city + district
          }
        })
      }
    } else if (
      (data.storeType == 2 && data.storeAccountType == 2) ||
      (data.storeType == 2 && data.storeAccountType == 3) ||
      (data.storeType == 3 && data.storeAccountType == 2)
    ) {
      state.accountName2 = data.storeAccount.accountName
      state.accountNum2 = data.storeAccount.accountNo
      state.bankId2 = data.storeAccount.bankIdHo
      state.branchBankId2 = data.storeAccount.bankId
      state.accountMobile2 = data.storeAccount.bankAccountMobile

      // 私户总行
      if (data.storeAccount.bankIdHo && data.storeAccount.bankIdHo != '0') {
        getBankDetail(data.storeAccount.bankIdHo).then((res4) => {
          if (res4.code == 200) {
            state.bankId2Text = res4.data.bankName
          }
        })
      }
      // 私户支行
      if (data.storeAccount.bankId && data.storeAccount.bankId != '0') {
        getBranchBankDetail(data.storeAccount.bankId).then((res5) => {
          if (res5.code == 200) {
            state.branchBankId2Text = res5.data.bankName
          }
        })
      }
      // 私户省市
      if (data.storeAccount.accountProvince) {
        state.acconutArea2 = [
          data.storeAccount.accountProvince,
          data.storeAccount.accountCity,
          data.storeAccount.accountDistrict,
        ]
        getAreatext({
          provinceCode: data.storeAccount.accountProvince,
          cityCode: data.storeAccount.accountCity,
          districtCode: data.storeAccount.accountDistrict,
        }).then((res6) => {
          if (res6.code == 200) {
            const { province } = res6.data
            const { city } = res6.data
            const { district } = res6.data
            state.acconutArea2Text = province + city + district
          }
        })
      }
    } else if (
      (data.storeType == 1 && data.storeAccountType == 1) ||
      (data.storeType == 2 && data.storeAccountType == 1)
    ) {
      state.accountName1 = data.storeAccount.accountName
      state.accountNum1 = data.storeAccount.accountNo
      state.bankId1 = data.storeAccount.bankIdHo
      state.branchBankId1 = data.storeAccount.bankId
      state.accountMobile1 = data.storeAccount.bankAccountMobile

      // 公户总行
      if (data.storeAccount.bankIdHo && data.storeAccount.bankIdHo != '0') {
        getBankDetail(data.storeAccount.bankIdHo).then((res2) => {
          if (res2.code == 200) {
            state.bankId1Text = res2.data.bankName
          }
        })
      }
      // 公户支行
      if (data.storeAccount.bankId && data.storeAccount.bankId != '0') {
        getBranchBankDetail(data.storeAccount.bankId).then((res3) => {
          if (res3.code == 200) {
            state.branchBankId1text = res3.data.bankName
          }
        })
      }
      // 公户省市
      if (data.storeAccount.accountProvince) {
        state.acconutArea2 = [
          data.storeAccount.accountProvince,
          data.storeAccount.accountCity,
          data.storeAccount.accountDistrict,
        ]
        getAreatext({
          provinceCode: data.storeAccount.accountProvince,
          cityCode: data.storeAccount.accountCity,
          districtCode: data.storeAccount.accountDistrict,
        }).then((res6) => {
          if (res6.code == 200) {
            const { province } = res6.data
            const { city } = res6.data
            const { district } = res6.data
            state.acconutArea1Text = province + city + district
          }
        })
      }
    }
  },
}

const actions = {}

export default {
  namespace: true,
  state,
  mutations,
  actions,
}
